/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.zt.dangyuan;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MenuItem;


import com.viewpagerindicator.IconPagerAdapter;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 */
public class ActivityLife extends BaseActivity {
    ViewPager viewPager;
    private String[] mTitles = new String[]{"贷款理财", "进出口商品", "生活快讯", "旅游生活", "生活娱乐"};
    private String[] cids = new String[]{"dklc", "jcksp", "shkx", "lysh", "shyl"};
    private int[] mIcons = new int[]{R.drawable.shfwicon_01, R.drawable.shfwicon_02, R.drawable.shfwicon_03, R.drawable.shfwicon_04, R.drawable.shfwicon_05};
    private TabPageIndicator mIndicator;
    FragmentAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addCenterView(R.layout.main_activity_life);
        setTitleImg(R.drawable.title_shenghuo);
        toolbar.setNavigationIcon(R.drawable.icon_select_all_light);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        mIndicator = (TabPageIndicator) findViewById(R.id.indicator);

        initDates();

    }

    private void initDates() {
        if (viewPager != null) {
            setupViewPager(viewPager);
        }
    }

    private void setupViewPager(ViewPager viewPager) {
        List<Fragment> fragments = initFragments();
        adapter = new FragmentAdapter(fragments, getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        mIndicator.setViewPager(viewPager);

    }

    private List<Fragment> initFragments() {
        List<Fragment> fragments = new ArrayList<Fragment>();
        for (int i = 0; i < mTitles.length; i++) {
            MyFragment1 userFragment = MyFragment1.newInstance(BBSDetailActivity.TYPE_BAOMING);
            userFragment.setTitle(mTitles[i]);
            userFragment.setIconId(mIcons[i]);
            userFragment.setCid(cids[i]);
            userFragment.setPos(1);
            fragments.add(userFragment);
        }
        return fragments;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                startActivity(new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    class FragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
        private List<Fragment> mFragments;

        public FragmentAdapter(List<Fragment> fragments, FragmentManager fm) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int i) {
            return mFragments.get(i);
        }

        @Override
        public int getIconResId(int index) {

            return mIcons[index];

        }

        @Override
        public int getCount() {

            return mTitles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {


            return mTitles[position];
        }
    }

}
